<template>
  <div class="index">
    <!-- 头部搜索区域 -->
    <div class="header">
      <div class="location">云南大学</div>
      <div class="search-box">
        <input type="text" placeholder="搜索商家、商品名称">
      </div>
    </div>

    <!-- 分类导航区域 -->
    <div class="category-container">
      <div class="category-list">
        <div class="category-item" v-for="item in categoryList" :key="item.id" @click="goToBusinessList(item.id)">
          <img :src="item.img" :alt="item.name">
          <span>{{ item.name }}</span>
        </div>
      </div>
    </div>

    <!-- 推荐商家区域 -->
    <div class="business-container">
      <div class="title">推荐商家</div>
      <div class="filter-bar">
        <span class="active">综合排序</span>
        <span>距离最近</span>
        <span>销量最高</span>
        <span>筛选</span>
      </div>
      <div class="business-list">
        <div class="business-item" v-for="item in businessList" :key="item.businessId" @click="goToBusinessInfo(item.businessId)">
          <div class="business-img">
            <img :src="item.businessImg" :alt="item.businessName">
          </div>
          <div class="business-info">
            <h3>{{ item.businessName }}</h3>
            <div class="business-desc">{{ item.businessExplain }}</div>
            <div class="business-delivery">
              <span>起送￥{{ item.starPrice }}</span>
              <span>配送￥{{ item.deliveryPrice }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部导航栏 -->
    <div class="footer">
      <div class="tab-item active">
        <i class="iconfont icon-home"></i>
        <span>首页</span>
      </div>
      <div class="tab-item" @click="$router.push('/orderList')">
        <i class="iconfont icon-order"></i>
        <span>订单</span>
      </div>
      <div class="tab-item" @click="$router.push('/login')">
        <i class="iconfont icon-user"></i>
        <span>我的</span>
      </div>
<!--      <div class="tab-item" @click="addNewItem">-->
<!--        <i class="iconfont icon-plus">+</i>-->
<!--        <span>添加</span>-->
<!--      </div>-->
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const businessList = ref([])

// 分类数据
const categoryList = ref([
  { id: 1, name: '美食', img: '/img/Typeimg/dcfl01.png' },
  { id: 2, name: '超市早餐', img: '/img/Typeimg/dcfl02.png' },
  { id: 3, name: '跑腿代购', img: '/img/Typeimg/dcfl03.png' },
  { id: 4, name: '汉堡披萨', img: '/img/Typeimg/dcfl04.png' },
  { id: 5, name: '甜品饮品', img: '/img/Typeimg/dcfl05.png' },
  { id: 6, name: '速食简餐', img: '/img/Typeimg/dcfl06.png' },
  { id: 7, name: '地方小吃', img: '/img/Typeimg/dcfl07.png' },
  { id: 8, name: '米饭面馆', img: '/img/Typeimg/dcfl08.png' },
  { id: 9, name: '包子粥铺', img: '/img/Typeimg/dcfl09.png' },
  { id: 10, name: '炸鸡炸串', img: '/img/Typeimg/dcfl10.png' }
])


// 获取商家列表
const getBusinessList = async () => {
  try {
    const res = await axios.get('http://localhost:8080/elm/business/listBusinessByOrderTypeId', {
      params: { orderTypeId: 1 }
    })
    businessList.value = res.data
  } catch (error) {
    console.error('获取商家列表失败:', error)
  }
}

// 跳转到商家列表页
const goToBusinessList = (typeId) => {
  router.push({
    path: '/businessList',
    query: { typeId }
  })
}

// 跳转到商家详情页
const goToBusinessInfo = (businessId) => {
  router.push({
    path: '/businessInfo',
    query: { businessId }
  })
}

onMounted(() => {
  getBusinessList()
})
</script>

<style scoped>
.index {
  padding-bottom: 50px;
}

.header {
  background: #0097ff;
  color: #fff;
  padding: 10px 15px;
}

.location {
  font-size: 18px;
  margin-bottom: 10px;
}

.search-box {
  background: #fff;
  border-radius: 20px;
  padding: 5px 10px;
}

.search-box input {
  width: 100%;
  border: none;
  outline: none;
  font-size: 14px;
}

.category-container {
  padding: 15px;
  background: #fff;
}

.category-list {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.category-item img {
  width: 40px;
  height: 40px;
}

.category-item span {
  font-size: 12px;
  color: #333;
  margin-top: 5px;
}

.business-container {
  margin-top: 10px;
  background: #fff;
}

.title {
  padding: 15px;
  font-size: 16px;
  font-weight: bold;
}

.filter-bar {
  display: flex;
  padding: 10px 15px;
  border-bottom: 1px solid #eee;
}

.filter-bar span {
  flex: 1;
  text-align: center;
  font-size: 14px;
  color: #666;
}

.filter-bar span.active {
  color: #0097ff;
}

.business-list {
  padding: 0 15px;
}

.business-item {
  display: flex;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.business-img {
  width: 80px;
  height: 80px;
  margin-right: 10px;
}

.business-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.business-info {
  flex: 1;
}

.business-info h3 {
  font-size: 16px;
  margin: 0 0 5px;
}

.business-desc {
  font-size: 12px;
  color: #666;
  margin-bottom: 5px;
}

.business-delivery {
  font-size: 12px;
  color: #999;
}

.business-delivery span {
  margin-right: 10px;
}

.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: #fff;
  display: flex;
  border-top: 1px solid #eee;
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #666;
}

.tab-item.active {
  color: #0097ff;
}

.tab-item i {
  font-size: 20px;
  margin-bottom: 3px;
  color: #0097ff;
}
</style>